<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/13
 * Time: 14:15
 */

namespace  Controller;

class OrderController extends TooController{
    //必须登录才能调用方法
    public function __construct()
    {
        //执行父类
        parent::__construct();

        //判断是否登录
        if(empty($_SESSION['Home']['Member']['id'])){
            $this->loca('Login','login'); //跳转登录
        }
    }

    //购物车列表--购物流程1
    public function index(){
        //查询购物车数据

        //查询购物车里面的数据
        $sql="select f.*,g.name,g.img,g.money,gg.gname,gg.gmoney,g.kc from s_flow as f join s_goods as g on f.g_id = g.id join s_goods_g as gg on f.gg_id = gg.id where m_id = '{$_SESSION['Home']['Member']['id']}'";


        $flow=$this->pdo->pdo_sel($sql,2);
        $this->view->assign('arr',$flow);

        $this->view->display();
    }

    //修改商品数量
    public function flow_edit(){
        $id=$_GET['id'];
        $num=$_GET['num'];
        //修改数量
        if($this->pdo->pdo_upd(array('num'=>$num),'s_flow', " id = '$id'")){
            //计算价格
            $sql="select f.*,g.name,g.img,g.money,gg.gname,gg.gmoney,g.kc from s_flow as f join s_goods as g on f.g_id = g.id join s_goods_g as gg on f.gg_id = gg.id where f.id = '{$id}'";
            $arr=$this->pdo->pdo_sel($sql);
            $jg=($arr['money']+$arr['gmoney'])*$arr['num'];

            $this->retuAjax(array('type'=>1,'msg'=>$jg));
        }else{
            $this->retuAjax(array('type'=>2,'msg'=>'服务器错误'));
        }
    }

    //删除操作
    public function flow_del(){
        $id=$_GET['id'];
        if($id){
            if($this->pdo->pdo_del("s_flow"," id = '$id'")){
                $this->retuAjax(array('msg'=>'删除成功','type'=>1));
            }else{
                $this->retuAjax(array('msg'=>'删除失败','type'=>2));
            }
        }
        $this->retuAjax(array('msg'=>'非法操作','type'=>2));
    }

    //购物流程2
    public function index2(){
        $id=trim($_GET['id'],','); //得到接收数据id
        if(empty($id)){
            $this->success('非法操作!谢谢','Order','index');
        }

        //查询数据
        $sql="select f.*,g.name,g.img,g.money,gg.gname,gg.gmoney,g.kc from s_flow as f join s_goods as g on f.g_id = g.id join s_goods_g as gg on f.gg_id = gg.id where f.id in ({$id})";

        $arr=$this->pdo->pdo_sel($sql,2);
        $this->view->assign('arr',$arr);

        //收货地址
        $dz=$this->pdo->pdo_sel("select * from s_dz where m_id = '{$_SESSION['Home']['Member']['id']}'",2);
        $this->view->assign('dz',$dz);
        $this->view->display();
    }

    //添加地址
    public function dz_add(){
        if($_POST){
            //提交地址
            $arr=array();
            $arr['name']=$_POST['name'];
            $arr['phone']=$_POST['phone'];
            $arr['dizhi']=$_POST['dizi'];
            $arr['dizhi_msg']=$_POST['dizhi_msg'];
            $arr['yzcode']=$_POST['yzcode'];
            $arr['m_id']=$_SESSION['Home']['Member']['id'];
            if($this->pdo->pdo_ins($arr,'s_dz')){
                $this->retuAjax(array('type'=>1,'msg'=>'提交成功'));
            }else{
                $this->retuAjax(array('type'=>2,'msg'=>'提交失败'));
            }
        }
        $this->view->display();
    }
    //删除地址
    public function dz_del(){
        $id=$_GET['id'];
        if($id){
            if($this->pdo->pdo_del("s_dz"," id = '$id' limit 1")){
                $this->retuAjax(array('type'=>1,'msg'=>'删除成功'));
            }else{
                $this->retuAjax(array('type'=>2,'msg'=>'删除失败'));
            }
        }
        $this->retuAjax(array('type'=>2,'msg'=>'非法请求'));
    }

    //修改地址
    public function dz_edit(){
        if($_POST){
            //提交地址
            $arr=array();
            $arr['name']=$_POST['name'];
            $arr['phone']=$_POST['phone'];

            if($_POST['dizi']!='请选择|请选择|请选择|'){
                $arr['dizhi']=$_POST['dizi'];
            }

            $arr['dizhi_msg']=$_POST['dizhi_msg'];
            $arr['yzcode']=$_POST['yzcode'];

            if($this->pdo->pdo_upd($arr,'s_dz'," id = '{$_POST['id']}'")){
                $this->retuAjax(array('type'=>1,'msg'=>'提交成功'));
            }else{
                $this->retuAjax(array('type'=>2,'msg'=>'提交失败'));
            }
        }
        $id=$_GET['id'];
        if($id){
            $arr=$this->pdo->pdo_sel("select * from s_dz where id = $id");
            $this->view->assign('arr',$arr);
            $this->view->display('dz_add');
        }
    }

    //购物流程3 --- 生成订单
    public function index3(){
        if($_GET['id']){
            //传递的是订单id
            $sql="select * from s_order where id = '{$_GET['id']}'";
            $list=$this->pdo->pdo_sel($sql);
        }else{
            $dz=$_POST['dz'];
            $pay=$_POST['pay'];
            $flow=trim($_POST['flow'],',');//购物车id

            //提交数据到订单基本表
            //1.查询购物车里面的数据
            //查询数据
            $sql="select f.*,g.name,g.img,g.money,gg.gname,gg.gmoney,g.kc from s_flow as f join s_goods as g on f.g_id = g.id join s_goods_g as gg on f.gg_id = gg.id where f.id in ($flow)";
            $arr=$this->pdo->pdo_sel($sql,2);
            $money=0;
            foreach ($arr as $v){
                $money+=($v['money']+$v['gmoney'])*$v['num'];
            }

            //地址信息
            $path=$this->pdo->pdo_sel("select * from s_dz where id = '$dz'");

            $list=array();
            $list['m_id']=$_SESSION['Home']['Member']['id'];
            $list['pay']=$_POST['pay'];
            $list['start']=1;
            $list['ordersn']=date("YmdHis",time()).rand(10,99);  //订单号,必须唯一
            $list['time']=date("Y-m-d H:i:s",time());
            $list['path']=$path['name'].'|'.$path['phone'].'|'.$path['dizhi'].'|'.$path['dizhi_msg'].'|'.$path['yzcode'];
            $list['money']=$money;
            $o_id=$this->pdo->pdo_ins($list,'s_order');
            $this->view->assign('o_id',$o_id);
            //生成订单详情记录
            foreach ($arr as $v){
                $iop=array();
                $iop['g_id']=$v['g_id'];
                $iop['gg_id']=$v['gg_id'];
                $iop['o_id']=$o_id;
                $iop['money']=$v['gmoney']+$v['moeny'];
                $iop['num']=$v['num'];
                $o_id=$this->pdo->pdo_ins($iop,'s_order_s');
            }
            //清除购物车里面的数据
            $this->pdo->pdo_del('s_flow'," id in($flow)");
        }
        $this->view->assign('list',$list);
        $this->view->display();
    }

    //订单支付方法
    public function pay(){
        $order_id=$_POST['order_id']; //得到订单id
        //查询订单数据
        $order=$this->pdo->pdo_sel("select * from s_order where id ='$order_id'");

        if($order['pay']=='支付宝支付'){
           //支付宝支付
            $this->zfbPay($order['ordersn'],'商品购买','0.01','测试商品购买');
        }else{
            //余额支付
        }
    }

    //前台跳转地址
    public function payY(){
        if($this->zfbPay_q()){
            //修改订单状态
            $arr=array();
            $arr['start']='2';
            $arr['zfbsn']=$_GET['trade_no'];
            $this->pdo->pdo_upd($arr,"s_order"," ordersn = '{$_GET['out_trade_no']}'");

            $this->success("支付成功",'Member','orderlist');
        }else{
            $this->success("支付失败",'Member','orderlist');
        }
    }

    //异步通知方法---支付宝的服务器会请求的方法(不需要登录也能够访问)
//    public function payX(){
        //这里面才去实现修改订单的操作
        // $arr=array();
        ////            $arr['start']='2';
        ////            $arr['zfbsn']=$_GET['trade_no'];
        ////            $this->pdo->pdo_upd($arr,"s_order"," ordersn = '{$_GET['out_trade_no']}'");
//    }



}